package com.tengju.user.infrastructure.repository.club;

import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.model.club.ClubZoneRelation;
import com.tengju.user.domain.model.club.ClubZoneRelationRepository;
import com.tengju.user.domain.model.club.ZoneId;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ClubZoneRelationDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.ClubZoneRelationDOCustomMapper;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.ClubZoneRelationDOMapper;
import com.tengju.user.infrastructure.repository.club.convert.ClubConvert;
import com.tengju.user.infrastructure.repository.club.convert.ClubZoneRelationConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/1/13
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ClubZoneRelationRepositoryImpl implements ClubZoneRelationRepository {


    private final ClubZoneRelationDOCustomMapper clubZoneRelationDOCustomMapper;
    private final ClubZoneRelationDOMapper clubZoneRelationDOMapper;


    @Override
    public List<ClubZoneRelation> findByZoneId(ZoneId zoneId) {
        List<ClubZoneRelationDO> clubZoneRelationDOList = clubZoneRelationDOCustomMapper.findByZoneId(zoneId.getValue());
        if (CollectionUtils.isEmpty(clubZoneRelationDOList)) {
            return Collections.emptyList();
        }
        return clubZoneRelationDOList.stream()
                .map(ClubZoneRelationConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public ClubZoneRelation findByClubId(ClubId clubId) {
        ClubZoneRelationDO clubZoneRelationDO = clubZoneRelationDOCustomMapper.findByClubId(clubId.getValue());
        return ClubConvert.relationToModel(clubZoneRelationDO);
    }

    @Override
    public ZoneId findZoneIdByClubId(ClubId clubId) {
        Long zoneId = clubZoneRelationDOCustomMapper.findZoneIdByClubId(clubId.getValue());
        return new ZoneId(zoneId);
    }

    @Override
    public void updateRelation(ClubZoneRelation clubZoneRelation) {
        ClubZoneRelationDO clubZoneRelationDO =  ClubConvert.modelToRelation(clubZoneRelation);
        clubZoneRelationDOCustomMapper.updateByClubId(clubZoneRelationDO);
    }

    @Override
    public void save(ClubZoneRelation clubZoneRelation) {
        ClubZoneRelationDO clubZoneRelationDO =  ClubConvert.modelToRelation(clubZoneRelation);
        clubZoneRelationDOMapper.insertSelective(clubZoneRelationDO);
    }


    @Override
    public List<ClubZoneRelation> batchGetByClubIds(List<ClubId> clubIds) {
        List<Long> clubIdValues = clubIds.stream().map(ClubId::getValue).collect(Collectors.toList());
        List<ClubZoneRelationDO> clubZoneRelationDOList = clubZoneRelationDOCustomMapper.batchGetByClubIds(clubIdValues);
        if (CollectionUtils.isEmpty(clubZoneRelationDOList)) {
            return Collections.emptyList();
        }
        return clubZoneRelationDOList.stream()
                .map(ClubZoneRelationConvert::convert)
                .collect(Collectors.toList());
    }

}
